<script setup lang="ts">
import { useOperator } from "./columns";
import { PureTableBar } from "@/components/RePureTableBar";
import { useRenderIcon } from "@/components/ReIcon/src/hooks";
import UserDialog, { initRoleDialog } from "./component/userDialog.vue";
import RoleLinker, { initLinker } from "./component/roleLinker.vue";
import DictSelector from "@/components/ReDict/src/dictSelector";
import { ref } from "vue";
import { getDeptTree } from "@/api/dept";
import { DeptModel } from "@/api/model/deptModel";
import { message } from "@/utils/message";
import { deleteUser } from "@/api/user";

defineOptions({
  name: "User",
});
const {
  form,
  formRef,
  loading,
  columns,
  dataList,
  pagination,
  onSearch,
  resetForm,
  handleSizeChange,
  handleCurrentChange,
} = useOperator();

const { openAdd } = initRoleDialog();
const { openLinker } = initLinker();

const handleDelete = row => {
  deleteUser(row.id)
    .catch(err => {
      message(err.message, { type: "error" });
    })
    .finally(() => {
      onSearch();
    });
};

const rootDept = ref<DeptModel[]>([
  {
    id: "0",
    label: "根节点",
  },
]);

const loadDeptTree = async (node, resolve) => {
  if (node && node.level === 0) {
    resolve(rootDept.value);
  } else {
    const data = await getDeptTree(node.data.id);
    resolve(data);
  }
};

const treeNodeClick = node => {
  form.deptId = node.id;
  onSearch();
};
</script>

<template>
  <div class="main">
    <div class="w-[17%] float-left">
      <div class="h-full bg-bg_color overflow-auto">
        <div class="flex items-center h-[34px]">
          <p class="flex-1 ml-2 font-bold text-base truncate" title="部门列表">
            部门列表
          </p>
        </div>
        <el-divider />
        <el-tree
          node-key="id"
          :props="{
            value: 'id',
            label: 'label',
            children: 'children',
          }"
          :load="loadDeptTree"
          :default-expanded-keys="['0']"
          :expand-on-click-node="false"
          @node-click="treeNodeClick"
          highlight-current
          lazy
        />
      </div>
    </div>
    <div class="float-right w-[81%]">
      <el-form
        ref="formRef"
        :inline="true"
        :model="form"
        class="bg-bg_color w-[99/100] pl-8 pt-4"
      >
        <el-form-item label="用户名" prop="username">
          <el-input
            v-model="form.username"
            placeholder="请输入用户名"
            clearable
            class="!w-[200px]"
          />
        </el-form-item>
        <el-form-item label="昵称" prop="nickName">
          <el-input
            v-model="form.nickName"
            placeholder="请输入昵称"
            clearable
            class="!w-[200px]"
          />
        </el-form-item>
        <el-form-item label="手机" prop="phone">
          <el-input
            v-model="form.phone"
            placeholder="请输入手机"
            clearable
            class="!w-[200px]"
          />
        </el-form-item>
        <el-form-item label="邮箱" prop="email">
          <el-input
            v-model="form.email"
            placeholder="请输入邮箱"
            clearable
            class="!w-[200px]"
          />
        </el-form-item>
        <el-form-item label="状态：" prop="status">
          <!--<el-select v-model="form.status" placeholder="请选择状态" clearable>
            <el-option label="启用" value="1" />
            <el-option label="禁用" value="0" />
          </el-select>-->
          <DictSelector code="ACCOUNT_STATUS" v-model="form.status" />
        </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            :icon="useRenderIcon('ep:search')"
            :loading="loading"
            @click="onSearch"
          >
            搜索
          </el-button>
          <el-button
            :icon="useRenderIcon('ep:refresh')"
            @click="resetForm(formRef)"
          >
            重置
          </el-button>
        </el-form-item>
      </el-form>

      <PureTableBar title="用户列表" @refresh="onSearch">
        <template #buttons>
          <el-button
            type="primary"
            :icon="useRenderIcon('ri:add-circle-line')"
            @click="openAdd"
          >
            新增用户
          </el-button>
        </template>
        <template v-slot="{ size, checkList }">
          <pure-table
            border
            align-whole="center"
            showOverflowTooltip
            table-layout="auto"
            :loading="loading"
            :size="size"
            :data="dataList"
            :columns="columns"
            :checkList="checkList"
            :pagination="pagination"
            :paginationSmall="size === 'small' ? true : false"
            :header-cell-style="{
              background: 'var(--el-table-row-hover-bg-color)',
              color: 'var(--el-text-color-primary)',
            }"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
          >
            <template #operation="{ row }">
              <el-button
                class="reset-margin"
                link
                type="primary"
                :size="size"
                @click="openLinker(row)"
                :icon="useRenderIcon('material-symbols:add-link-rounded')"
              >
                分配角色
              </el-button>
              <el-popconfirm title="是否确认删除?" @confirm="handleDelete(row)">
                <template #reference>
                  <el-button
                    class="reset-margin"
                    link
                    type="primary"
                    :size="size"
                    :icon="useRenderIcon('ep:delete')"
                  >
                    删除
                  </el-button>
                </template>
              </el-popconfirm>
            </template>
          </pure-table>
        </template>
      </PureTableBar>
      <UserDialog @reload="onSearch" />
      <RoleLinker />
    </div>
  </div>
</template>

<style scoped lang="scss">
:deep(.el-dropdown-menu__item i) {
  margin: 0;
}

:deep(.el-divider) {
  margin: 0;
}
</style>
